package com.unittec.drainage.provider.auth.util;

import com.unittec.drainage.provider.auth.dto.SysPermissionTree;
import com.unittec.drainage.provider.auth.entity.SysPermission;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: scott
 * @Date: 2019-04-03
 */
public class PermissionDataUtil {

    /**
     * 智能处理错误数据，简化用户失误操作
     * 
     * @param permission
     */
    public static SysPermission intelligentProcessData(SysPermission permission) {
        if (permission == null) {
            return null;
        }

        // 组件
        if (StringUtils.isNotEmpty(permission.getComponent())) {
            String component = permission.getComponent();
            if (component.startsWith("/")) {
                component = component.substring(1);
            }
            if (component.startsWith("views/")) {
                component = component.replaceFirst("views/", "");
            }
            if (component.startsWith("src/views/")) {
                component = component.replaceFirst("src/views/", "");
            }
            if (component.endsWith(".vue")) {
                component = component.replace(".vue", "");
            }
            permission.setComponent(component);
        }

        // 请求URL
        if (StringUtils.isNotEmpty(permission.getPath())) {
            String url = permission.getPath();
            if (url.endsWith(".vue")) {
                url = url.replace(".vue", "");
            }
            if (!url.startsWith("http") && !url.startsWith("/") && !url.trim().startsWith("{{")) {
                url = "/" + url;
            }
            permission.setPath(url);
        }

        // 一级菜单默认组件
        if (0 == permission.getMenuType() && StringUtils.isBlank(permission.getComponent())) {
            permission.setComponent("layouts/RouteView");
        }
        return permission;
    }

    /**
     * 添加首页
     * 
     * @param list
     */
    public static void addIndexPage(List<SysPermissionTree> list) {
        if (CollectionUtils.isEmpty(list)) {
            SysPermissionTree tree = new SysPermissionTree();
            tree.setName("home");
            tree.setNameZh("首页");
            tree.setPath("/");
            tree.setComponent("Layout");
            tree.setRedirect("dashboard");
            tree.setMenuType(0);
            tree.setSortNo(0);
            tree.setAlwaysShow(false);
            tree.setIcon("dashboard");
            tree.setRoute(true);
            tree.setLeaf(false);
            tree.setKeepAlive(false);
            tree.setHidden(false);
            tree.setDelFlag(0);
            tree.setRuleFlag(0);
            tree.setStatus(0);
            List<SysPermissionTree> children = new ArrayList<SysPermissionTree>();
            SysPermissionTree childrenTree = new SysPermissionTree();
            childrenTree.setName("Dashboard");
            childrenTree.setNameZh("首页");
            childrenTree.setPath("dashboard");
            childrenTree.setComponent("dashboard/index");
            childrenTree.setMenuType(1);
            childrenTree.setSortNo(0);
            childrenTree.setAlwaysShow(false);
            childrenTree.setIcon("dashboard");
            childrenTree.setRoute(true);
            childrenTree.setLeaf(true);
            childrenTree.setKeepAlive(false);
            childrenTree.setHidden(false);
            childrenTree.setDelFlag(0);
            childrenTree.setRuleFlag(0);
            childrenTree.setStatus(0);
            children.add(childrenTree);
            tree.setChildren(children);
            list.add(tree);
        }
    }

}
